package jee.boot.entity.auth;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jee.boot.common.annotation.Column;
import jee.boot.common.annotation.Table;
import jee.boot.common.annotation.Transient;
import jee.boot.common.basic.DataEntity;
import jee.boot.common.utils.JsonMapper;
import jee.boot.common.utils.StringUtils;

import java.util.Map;

/**
 * 用户自定义数据
 */
@Table(name="at_user_profile")
public class UserProfile extends DataEntity<UserProfile> {
    /**
     * 用户
     */
    @Column(data = {"user.id","user.name"})
    private User user;
    /**
     * 编码
     */
    private String code;
    /**
     * 数据
     */
    private String configJson;
    @Transient
    private Map<String,Object> config;

    public UserProfile() {
    }

    public UserProfile(String id) {
        super(id);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
    @JsonIgnore
    public String getConfigJson() {
        return configJson;
    }

    public void setConfigJson(String configJson) {
        this.configJson = configJson;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Map<String, Object> getConfig() {
        if(StringUtils.isNotBlank(configJson)){
            config= JsonMapper.fromJsonString(configJson,Map.class);
        }
        return config;
    }

    public void setConfig(Map<String, Object> config) {
        this.config = config;
        if(config!=null){
            configJson=JsonMapper.toJsonString(config);
        }
    }
}
